﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.Data;
using System.Data.SqlClient;
using System.Text;

namespace FinalWeb
{
    public partial class UserDiary : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            this.lblUserIN.Text = "您好！" + Session["UserName"].ToString();
        }

        protected void btnAll_Click(object sender, EventArgs e)
        {
            Response.Redirect("UserLogin.aspx");
        }

        protected void btnRe_Click(object sender, EventArgs e)
        {
            this.txtTitle.Text= "";
            this.txtTime.Text = "";

            this.DropDownList4.Text = "";
            this.txtEvent.Text = "";
            
        }

        protected void btnToday_Click(object sender, EventArgs e)
        {
            string strSql = "SELECT   UserId, title, time, event, mood FROM   UserDiary WHERE   (UserId = @UserId) AND (time = @time)";
            SqlParameter[] pms = new SqlParameter[2];
            pms[0] = new SqlParameter("@UserId", Session["UserName"]);
            pms[1] = new SqlParameter("@time", this.txtTime.Text);
            SqlDataReader dr = WebApp.SqlHelper.ExecuteReader(strSql, pms);
            if (dr.Read())
            {
                this.txtTitle.Text = dr["title"].ToString();
                this.DropDownList4.Text = dr["mood"].ToString();
                this.txtEvent.Text = dr["event"].ToString();

            } dr.Close();
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string strCn = "server=pc.hstc.cn;database=dhd2014112102;uid=2014112102;pwd=19960523;";
            SqlConnection cn = new SqlConnection(strCn);
            try
            {
                cn.Open();
                StringBuilder SqlStringBuilder = new StringBuilder(1024);
                SqlStringBuilder.Append("INSERT INTO UserDiary ");
                SqlStringBuilder.Append("                (title, time,event, mood) ");
                SqlStringBuilder.Append("VALUES   (@title,@time,@event,@mood) ");

                string strSql = SqlStringBuilder.ToString();
                SqlCommand cmd = new SqlCommand(strSql, cn);

                SqlParameter pmstitle = new SqlParameter("@title", this.txtTitle.Text);
                SqlParameter pmstime = new SqlParameter("@time", this.txtTime.Text );
                SqlParameter pmsevent = new SqlParameter("@event", this.txtEvent.Text);
                SqlParameter pmsmood = new SqlParameter("@mood", this.DropDownList4.Text);


                cmd.Parameters.Add(pmstitle);
                cmd.Parameters.Add(pmstime);
                cmd.Parameters.Add(pmsevent);
                cmd.Parameters.Add(pmsmood);

                int iAdd = cmd.ExecuteNonQuery();
                if (iAdd < 1)
                {
                    this.lblMessage.Text = "保存日记成功！";
                }
                else
                {
                    this.lblMessage.Text = "保存了" + iAdd.ToString() + "条新日记！祝您今天有一个好心情";
                }
            }
            catch (Exception ex)
            {
                this.lblMessage.Text = ex.Message;
            }
            finally
            {
                cn.Close();
            }
        }

        protected void DropDownList5_SelectedIndexChanged(object sender, EventArgs e)
        {
            string url = Request.Path + "?Theme=" + DropDownList5.SelectedItem.Value;
            Response.Redirect(url);
        }
        protected void Page_PreInit(object sender, EventArgs e)
        {
            if (Request.QueryString["Theme"] == null)
            {
                Theme = "日常";
            }
            else
            {
                Theme = Request.QueryString["Theme"];
            }
            Page.Theme = Theme;
            ListItem item = DropDownList5.Items.FindByValue(Theme);
            if (item != null)
            {
                item.Selected = true;
            }
        }

        protected void btnO_Click(object sender, EventArgs e)
        {
            Response.Write(@"<script language=javascript>alert('注销成功，欢迎下次登陆！');window.window.location.href='UserLogin.aspx';</script>"); 
        }

        protected void Timer1_Tick(object sender, EventArgs e)
        {
            this.lblTime.Text = System.DateTime.Now.ToString();
        }
    }
}